home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12939 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.2 KB  |  42 lines

  1. Path: li.net!jeremy
  2. From: jeremy@newshost.li.net (Jeremy Markman)
  3. Newsgroups: comp.lang.c,comp.unix.programmer
  4. Subject: Re: Q: '\n' character
  5. Followup-To: comp.lang.c,comp.unix.programmer
  6. Date: 3 Apr 1996 16:38:09 GMT
  7. Organization: LI Net (Long Island Network)
  8. Distribution: world
  9. Message-ID: <4ju9hh$o93@linet06.li.net>
  10. References: <31616F63.481D@lava.weeg.uiowa.edu>
  11. NNTP-Posting-Host: linet04.li.net
  12. X-Newsreader: TIN [version 1.2 PL2]
  13.  
  14. Artur Wojdat (awojdat@lava.weeg.uiowa.edu) wrote:
  15. : Hello everybody,
  16. :     Is there a function or some sort of way that I could remove '\n' 
  17. : charecter form the end of the string. I'm reading from two files, want to 
  18. : form one line of text and then have it printed out to stdout. I use fgets to 
  19. : read from the file and I noticed that it appends newline char at the end.
  20.  
  21. You could write an rtrim() function that will remove all whitespace (i.e. 
  22. spaces, tabs, newline chars, etc) from the right side of your string.
  23.  
  24. Here's an example of the function:
  25.  
  26. #include <ctype.h>
  27.  
  28. char *rtrim(char *string)
  29.  {
  30.   int i = strlen(string) - 1;
  31.  
  32.   if (i < 0) return(string);
  33.  
  34.   while (isspace(string[i]) && i >= 0)
  35.    {
  36.     string[i] = '\0';
  37.     i--;
  38.    }
  39.  
  40.   return(string);
  41.  }
  42.